// 引入路由
import router from '@/router'
// 引入store
import store from '@/store'
// 进度条引入
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

const whiteList = ['/login', '/404']

// 判断 是否有token 来进行跳转
router.beforeEach(async (to, from, next) => {
    NProgress.start()
    if (store.getters.token) {
        if (to.path === '/login') {
            next('/')
        } else {
            if (!store.getters.userId) {
                await store.dispatch('user/getUserInfo')
            }
            next()
        }
        // to.path === '/login' ? next('/') : next()
    } else {
        whiteList.indexOf(to.path) >= 0 ? next() : next('/login')
    }
    NProgress.done()
})

router.afterEach(() => {
    NProgress.done()
})